
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/Sanpham.php';
$sanphamOb = new Sanpham();

$listSPDM = $sanphamOb->laySanPhamTheoDanhMuc($_GET['id']);
?>
<?php if (mysql_num_rows($listSPDM) > 0): ?>
    <?php
    $lsp = $_GET["id"];
    $result = DataProvider::GetRows("select * from sanpham where danhmuc_id = $lsp");
    if (!isset($_GET['page'])) {
        $page = 1;
    } else {
        $page = $_GET['page'];
    }
    $max_results = 9;
    $from = (($page * $max_results) - $max_results);
    $sql = DataProvider::ExecuteQuery("select * from sanpham where danhmuc_id = $lsp LIMIT $from, $max_results");
    while ($sp = DataProvider::ExecuteQueryArray($sql)) {
            ?>

            <div class="content_section">
                <div class="product_box">
                    <h3><?php echo $sp['ten'] ?></h3>
                    <div class="image_wrapper" ><a href="chitietsanpham.php?id=<?php echo $sp['id'] ?>" target="_parent"><img class="img" src="<?php echo $sp['hinhanh'] ?>" /></a> </div>
                    <p class="price">Giá: <?php echo number_format($sp['gia']) ?> VNĐ</p>
                    <a href="chitietsanpham.php?id=<?php echo $sp['id'] ?>">Chi tiết</a> | <a class="them-gio-hang" rel="<?php echo $sp['id'] ?>" href="javascript:void(0)">Chọn</a>
                </div>
            </div> <!-- end of templatmeo_content -->
            <script type="text/javascript">
                $(document).ready(function(){
                    $(".them-gio-hang").click(function(){
                        var pID = $(this).attr('rel'); 
                        var tenP = $("#ten_sp_"+pID).text();
                        // Ajax them vao gio hang san pham tuong ung
                        $.post(
                        '/ajax/themgiohang.php',
                        {
                            id:pID,
                            ten:tenP
                        },
                        function(data) {
                            alert(data);
                        }
                    )
                    });
                });
            </script>  
        <?php
    }
    ?>
    <div style="float: right; margin: 0px 325px 0px"><?php
    $total_results = DataProvider::TongSanPham("SELECT COUNT(*) as Num FROM sanpham where danhmuc_id = $lsp");
    $total_pages = ceil($total_results / $max_results);
    echo "<center>Trang<br/>";
    if ($page > 1) {
        $prev = ($page - 1);
        echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?id=$lsp&&page=$prev\"><<Trang trước</a>&nbsp;";
    }
    for ($i = 1; $i <= $total_pages; $i++) {
        if (($page) == $i) {
            echo "<b>$i&nbsp;</b>";
        } else {
            echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?id=$lsp&&page=$i\">$i</a>&nbsp;";
        }
    }
    ?></div>

    <?php else: ?>
    <p style="padding: 10px">Không có sản phẩm thuộc danh mục này!!!</p>
<?php endif; ?>

